package com.webserver.controller;

import com.webserver.entity.Article;
import com.webserver.entity.User;
import com.webserver.http.HttpServletRequest;
import com.webserver.http.HttpServletResponse;

import java.io.*;
import java.util.ArrayList;
import java.util.List;

public class ArticleController {
    private static File USER_DIR = new File("./articles");

    static {
        if (!USER_DIR.exists()) {
            USER_DIR.mkdirs();
        }
    }

    public void showAllArticle(HttpServletRequest request,HttpServletResponse response) {
        List<Article> articleList = new ArrayList<>();
        File[] subs = USER_DIR.listFiles(f -> f.getName().endsWith(".obj"));
        for (File file3 : subs) {
            try (ObjectInputStream ois = new ObjectInputStream(
                    new FileInputStream(file3))
            ) {
                Article article = (Article) ois.readObject();
                articleList.add(article);
            } catch (IOException | ClassNotFoundException e) {
                e.printStackTrace();
            }
        }

        File file = new File("./ArticleList.html");
        try (PrintWriter pw = new PrintWriter(file, "UTF-8");
        ) {
            pw.println("<!DOCTYPE html>");
            pw.println("<html lang=\"en\">");
            pw.println("<head>");
            pw.println("<meta charset=\"UTF-8\">");
            pw.println("<title>用户列表</title>");
            pw.println("</head>");
            pw.println("<body>");
            pw.println("<center>");
            pw.println("<h1>用户列表</h1>");
            pw.println("<table border=\"1\">");
            pw.println("<tr>");
            pw.println("<td>标题</td>");
            pw.println("<td>作者</td>");
            pw.println("</tr>");

            for (Article article: articleList) {
                pw.println("<tr>");
                pw.println("<td>"+article.getTitle()+"</td>");
                pw.println("<td>"+article.getAuthor()+"</td>");
                pw.println("</tr>");
            }
            pw.println("</table>");
            pw.println("</center>");
            pw.println("</body>");
            pw.println("</html>");



        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        response.setContentFile(file);
    }

    public void writeArticle(HttpServletRequest request, HttpServletResponse response) {
        String title = request.getParameter("title");//标题
        String author = request.getParameter("author");//作者
        String content = request.getParameter("content");//正文
//        System.out.println(title+","+author+","+content+"6666666666666666666666666666666");
        File file2 = new File(USER_DIR, title + ".obj");

        if (title == null || author == null || content == null){
            File file = new File("./webapps/myweb/article_fail.html");
            response.setContentFile(file);
            return;
        }

        Article article = new Article(title, author, content);
        try (FileOutputStream fos = new FileOutputStream(file2);
             ObjectOutputStream oos = new ObjectOutputStream(fos);
        ) {
            oos.writeObject(article);
            System.out.println("发表成功");
            File file = new File("./webapps/myweb/article_success.html");
            response.setContentFile(file);
        } catch (IOException e) {
        }
    }
}
